<!---
Name:        addeditprod.cfm
Author:      Dmitry Svito (prostotelo@gmail.com)
Description: add or edit product
Created:     25.03.2011
--->

<cfparam name="Categ" type="integer" default="0">
<cfparam name="AddFlag" type="integer" default="0">
<cfparam name="Action" type="string" default="addeditprod.cfm">
<cfparam name="Form.Operation" type="string" default="0">
<cfparam name="Form.ProdName" type="string" default=" ">
<cfparam name="Form.ProdPrice" type="string" default="">
<cfparam name="Form.ProdImg" type="string" default="">
<cfparam name="Form.ProdDesc" type="string" default="">
<cfparam name="ProductId" type="integer" default="0">
<cfparam name="Error" type="integer" default="0">
<cfparam name="ImgName" type="string" default="">

<cfparam name="PrNam" type="string" default="">
<cfparam name="PrPri" type="string" default="">
<cfparam name="PrImg" type="string" default="">
<cfparam name="PrDes" type="string" default="">

<!---Products--->
<cfset ProdObjGate	= createObject("component", "Catalog.DataAccessLayer.ProductsGateway").init() />
<cfset ProdObjDAO = createObject("component", "Catalog.DataAccessLayer.ProductsDAO").init() />

<cfif (NOT IsDefined("URL.prod") AND NOT IsDefined("URL.cat")) OR (NOT IsDefined("URL.add") AND NOT IsDefined("URL.cat"))>
	<cflocation url="categories.cfm">
<cfelseif IsDefined("URL.prod") AND IsDefined("URL.cat")>
	<cfset ProductId = "#URL.prod#">
	<cfset Categ = "#URL.cat#">
<cfelseif IsDefined("URL.add") AND IsDefined("URL.cat")>
	<cfset AddFlag = "#URL.add#">
	<cfset Categ = "#URL.cat#">	
</cfif>

<cfset ProductRow = ProdObjDAO.read(ProductId)>
<cfset PrNam = ProductRow.getName() >
<cfset PrPri = ProductRow.getPrice() >
<cfset PrImg = ProductRow.getImageName() >
<cfset PrDes = ProductRow.getDescription() >

<cfif AddFlag IS 1>
	<cfset Action = "addeditprod.cfm?add=#AddFlag#&cat=#Categ#">
<cfelse>
	<cfset Action = "addeditprod.cfm?prod=#ProductId#&cat=#Categ#">
</cfif>

<html>
<head>
	<title>
		Joe's Catalog
    	<cfif AddFlag IS 1>
			Add product
		<cfelse>
			Edit product
    	</cfif>
	</title>
</head>
<body>
<cfform name="addeditprodform" action="#Action#" method="post" enctype="multipart/form-data" accept-charset="utf-8">
<table width="30%" cellspacing="2" cellpadding="2" summary="table">
    <tr>
        <td colspan="2" align="center">
        	<cfif AddFlag IS 1>
				Add product
			<cfelse>
				Edit product
        	</cfif>
        </td>
    </tr>
    <tr>
    	<td align="right">
    		Name:
		</td>
        <td align="left">
			<cfinput type="text" name="ProdName" value="#PrNam#"
	            maxlength="24"
		        required="true"
		        message="Product name is required"
		        validateAt="onServer">			
        </td>
    </tr>
    <tr>
    	<td align="right">
    		Price:
		</td>
        <td align="left">
			<cfinput type="text" name="ProdPrice" value="#PrPri#"
	            maxlength="19"
		        required="true"
				validate="integer"
				message="Product price is required"
		        validateAt="onServer">						
        </td>
    </tr>
    <tr>
    	<td align="right">
    		Image:
		</td>
        <td align="left">
			<cfinput type="file" name="ProdImg"
			    maxlength="50"
		        required="false"
				message="Product image is required"
		        validateAt="onServer">									
        </td>
    </tr>
</table>
<table width=820 cellspacing="2" cellpadding="2">	
	<tr>
    	<td align="Left">
    		Description:
		</td>
    </tr>
	<tr>
	    <td align="Left">
	    	<cftextarea name="ProdDesc" value="#PrDes#" wrap="virtual" richtext="true"/>			
        </td>
	</tr>	
    <tr>
        <td align="Left">
       		<cfinput type="submit" name="Operation" value="Save">
        	<cfinput type="button" name="Operation" value="Cancel" onclick="goback(#Categ#)">
        </td>
    </tr>
</table>
</cfform>

<cfif Form.Operation IS "Save">
<cftry>
	<cfif Form.ProdImg IS NOT "">
		<cffile action="upload" filefield="Form.ProdImg"
		destination="c:\ColdFusion9\wwwroot\Catalog\images\#Form.ProdName#.jpg"
		accept="image/jpeg, image/gif" nameconflict="overwrite">
		<cfset ImgName = "#Form.ProdName#.jpg">
	</cfif>
	<cfcatch>
		<h4><cfoutput>#cfcatch.Detail#</cfoutput></h4>
		<cfset Error = 1>
		<!---<cfdump var="#cfcatch#" label="Error Info">---> 
	</cfcatch>
</cftry>
<cfif Error IS 0>
	<cfif AddFlag IS 1>
		<cfset ProdObjDAO.create(Categ, Form.ProdName, Form.ProdDesc, Form.ProdPrice, ImgName<!---"#Form.ProdName#.jpg"--->)>
	<cfelse>
		<cfset ProdObjDAO.update(ProductId, Form.ProdName, Form.ProdDesc, Form.ProdPrice, ImgName<!---"#Form.ProdName#.jpg"--->)>
	</cfif>
	<cflocation url="products.cfm?cat=#Categ#">
</cfif>
</cfif>
</body>
</html>
<script>
	function goback (CatId)
	{		
		location.replace("products.cfm?cat="+CatId);
	}
</script>
